package com.dev.receiver;

import com.dev.utils.LogUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.window.dialog.ToastDialog;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;

/**
 * 监听时区、时间变化的订阅公告事件类
 */
public class TimeSubscriber extends CommonEventSubscriber {

    private AbilitySlice slice;
    public static final String COMMON_EVENT_TIMEZONE_CHANGED = "usual.event.TIMEZONE_CHANGED";
    public static final String COMMON_EVENT_TIME_CHANGED = "usual.event.TIME_CHANGED";
    public static final String COMMON_EVENT_TIME_TICK = "usual.event.TIME_TICK";

    public TimeSubscriber(CommonEventSubscribeInfo subscribeInfo, AbilitySlice slice) {
        super(subscribeInfo);
        this.slice = slice;
    }

    @Override
    public void onReceiveEvent(CommonEventData commonEventData) {
        if (commonEventData == null || commonEventData.getIntent() == null) {
            return;
        }
        if (COMMON_EVENT_TIMEZONE_CHANGED.equals(commonEventData.getIntent().getAction())) {
            new ToastDialog(slice).setText("时区改变！").show();
            LogUtils.info("TIME", "timezone is changed!");
        } else if (COMMON_EVENT_TIME_CHANGED.equals(commonEventData.getIntent().getAction())) {
            new ToastDialog(slice).setText("时间改变！").show();
            LogUtils.info("TIME", "time is changed!");
        } else if (COMMON_EVENT_TIME_TICK.equals(commonEventData.getIntent().getAction())) {
            new ToastDialog(slice).setText("分钟改变！").show();
            LogUtils.info("TIME", "test!");
        }
    }
}
